package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    /**
     * 自定义菜品分页查询方法
     */
    @Override
    public Page<DishDto> selectPage(Integer page, Integer pageSize, String name) {
        //创建一个分页对象page,设置分页参数
        Page<DishDto> p = new Page<>(page, pageSize);
        //调用mapper层分页查询方法,将分页对象和查询条件封装进去
        Page<DishDto> pageInfo = dishMapper.selectPage(p, name);
        //将分页对象返回
        return pageInfo;

    }

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 自定义菜品添加方法
     *
     * @param dishDto
     */
    @Override
    public void add(DishDto dishDto) {
        //调用mapper层添加方法
        dishMapper.insert(dishDto);
        //获取Flavors口味集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (flavors!=null){
            //遍历获取每一个口味
            for (DishFlavor flavor : flavors) {
                //将Dish表中id取出给口味表关联字段DishId赋值
                flavor.setDishId(dishDto.getId());
            }
            //调用dishFlavorService层添加集合方法saveBatch
            dishFlavorService.saveBatch(flavors);
        }
    }

    /**
     * 自定义根据id查询方法
     *
     * @param id
     * @return
     */
    @Override
    public DishDto selectDishDtoById(Long id) {
        return dishMapper.selectDishDtoById(id);
    }

    /**
     * 自定义修改方法
     *
     * @param dto
     */
    @Override
    public void updateDishDto(DishDto dto) {
        //调用mapper层修改方法
        dishMapper.updateById(dto);
        //获取口味表
        List<DishFlavor> flavors = dto.getFlavors();
        //调用口味表修改方法 select * from dish_flavor where dish_id=?
        dishFlavorService.lambdaUpdate()
                //判断是否相等(判断是否有值)
                .eq(DishFlavor::getDishId, dto.getId())
                //有的话就删除
                .remove();
        //遍历口味表
        for (DishFlavor flavor : flavors) {
            //给每个口味设置dishId
            flavor.setDishId(dto.getId());
        }
        //调用添加集合方法将口味添加进去(因为数据库口味表这一项已经跟id删完了,所以不能调用修改方法只能添加)
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 自定义删除方法
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        Integer count = this.lambdaQuery()
                .in(ids != null, Dish::getId, ids)
                .eq(Dish::getStatus, 1)
                .count();
        if (count>0){
            throw new CustomException("商品在售中,不能删除");
        }
        //删除菜品表信息
        dishMapper.deleteBatchIds(Arrays.asList(ids));
        //删除口味表相关信息
        for (Long id : ids) {
            dishFlavorService.lambdaUpdate().eq(DishFlavor::getDishId,id).remove();
        }
    }

    /**
     * 自定义修改状态方法
     * @param status
     * @param ids
     */
    @Override
    public void updateByStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            this.lambdaUpdate().eq(Dish::getId,id).set(Dish::getStatus,status).update();
        }
    }



    @Override
    public List<DishDto> selectList(Long categoryId, String name) {

        return  dishMapper.selectBYList(categoryId,name);
    }

    /**
     * 自定义查询套餐信息
     * @param setmealId
     * @return
     */
    @Override
    public List<DishDto> selectSetmealDish(Long setmealId) {
        return dishMapper.selectSetmealDish(setmealId);
    }


}



